﻿
Imports System.Threading

Public Class tcpServerAngel
    Inherits tcpServer

    Private thread As Thread = Nothing

    Public Overrides Function health() As Boolean
#If PocketPC OrElse Smartphone Then
        Return MyBase.health() AndAlso Not thread Is Nothing
#Else
        Return MyBase.health() AndAlso Not thread Is Nothing AndAlso thread.IsAlive()
#End If
    End Function

    Public Sub serverAngel()
        If Not MyBase.start() Then
            raiseError("cannot start tcpServer, shutdown ...", errorHandle.errorType.system)
            [stop]()
        End If
        thread = Nothing
    End Sub

    Public Overloads Function start(ByVal port As Int32) As Boolean
        MyBase.port = port
        Return start()
    End Function

    Public Overloads Function start() As Boolean
        thread = New Thread(AddressOf serverAngel)
        thread.Name = "TCPSERVER_ANGEL"
        thread.Start()
#If PocketPC OrElse Smartphone Then
        While Not thread Is Nothing AndAlso Not active()
#Else
        While Not thread Is Nothing AndAlso thread.IsAlive() AndAlso Not active()
#End If
            sleep(16)
        End While
        Return True
    End Function

End Class
